<?php
/**
 * Response Class
 *
 * @package Extensions
 * @version @@VERSION@@
 * @subpackage Low-Cal_Vanilla
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */

/**
 * Response Class
 * @package Extensions
 * @subpackage Low-Cal_Vanilla
 */
class LowCalVanilla_Response {

	/**
	 * @var array
	 * @access private
	 */
	var $_Headers;

	/**
	 * Constructor
	 * @return void
	 * @param bool $Clear Should the buffer cleared
	 */
	function LowCalVanilla_Response($Clear = false) {
		$this->ClearHeader();
		if ($Clear) {
			$this->ClearBody();
		}
	}

	/**
	 * Add a pair Key/Value header
	 * @return void
	 * @param string $Key
	 * @param string $value
	 */
	function AddHeader($Key, $Value) {
		$Key = ForceString($Key, '');
		$Value = ForceString($Value, '');
		if (!$Key) {
			return false;
		}
		$this->_Headers[$Key] = $Value;
		return true;
	}

	/**
	 * Add content to the body.
	 * @return void
	 * @param string $Content
	 */
	function AppendBody($Content) {
		echo $Content;
	}

	/**
	 * Clear the Header (set with the response object).
	 * @return void
	 */
	function ClearHeader() {
		$this->_Headers = array();
	}

	/**
	 * Clear the body content.
	 * @return void
	 */
	function ClearBody() {
		ob_clean();
	}

	/**
	 * Clear body and header and sent a not modified response
	 * @return void
	 */
	function NotChanged() {
		$this->ClearBody();
		$this->ClearHeader();
		header($_SERVER['SERVER_PROTOCOL'] .' 304 Not Modified');
	}

	/**
	 * Clear body and header and sent a not found response
	 * @return void
	 */
	function NotFound() {
		$this->ClearBody();
		$this->ClearHeader();
		header($_SERVER['SERVER_PROTOCOL'] .' 404 Not Found');
	}

	/**
	 * Sent body - doesn't check if this object headers have been sent.
	 * @return void
	 */
	function SentBody() {
		ob_flush();
	}

	/**
	 * Sent header - return false if the header can't be sent.
	 * @return bool
	 */
	function SentHeader() {
		if (!headers_sent()) {
			foreach ($this->_Headers as $key => $var) {
				header($key . ': ' . $var);
			}
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Send headers and body
	 * @return void
	 */
	function SentResponse() {
		//$this->AddHeader('Content-Length', ob_get_length());
		$this->SentHeader();
		$this->SentBody();
	}

	/**
	 * Append headers
	 * @return void
	 * @param array $Headers
	 */
	function AppendHeaders($Headers = array()) {
		if (is_array($Headers)) {
			$this->_Headers = array_merge($Headers, $this->_Headers);
		}
	}

	/**
	 * Append headers
	 * @return void
	 * @param array $Headers
	 */
	function SetHeaders($Headers = array()) {
		if (is_array($Headers)) {
			$this->_Headers = $Headers;
		}
	}

	/**
	 * Set body
	 * @return void
	 * @param string $Content
	 */
	function SetBody($Content) {
		$this->ClearBody();
		$this->AppendBody($Content);
	}
}